﻿/*
 * fbasync - An Asynchronous Facebook API for .NET
 * Copyright (C) 2007 ittybittysoft
 * Email: info@ittybittysoft.com
 * Portions of the library are based on the Facebook Developer Toolkit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;

namespace fbasync
{
    public enum InfoSectionType
        : int
    {
        Unknown = 0,
        TextOnly = 1,
        Thumbnail = 5
    }

    [DataContract, Serializable, KnownType(typeof(InfoSectionType)), KnownType(typeof(InfoField)), KnownType(typeof(InfoItem))]
    public class InfoSection
        : FacebookEntity
    {
        [DataMember]
        public string title { get; set; }

        [DataMember]
        public IList<InfoField> info_fields { get; set; }

        [DataMember]
        public InfoSectionType type { get; set; }

        public static InfoSection FromFqlResult(XmlDocument fqlResult, bool keepXml)
        {
            XmlElement e = fqlResult.DocumentElement;
            return new InfoSection
                {
                    title = e.GetChildText("title"),
                    info_fields = InfoField.FromContainerElement(e["info_fields", FacebookConnection.FacebookNamespace]),
                    type = InfoSectionType.Unknown
                };
        }
    }

    [DataContract, Serializable, KnownType(typeof(InfoItem))]
    public class InfoField
        : FacebookEntity
    {
        [DataMember]
        public string field { get; set; }

        [DataMember]
        public IList<InfoItem> items { get; set; }

        public static IList<InfoField> FromContainerElement(XmlElement e)
        {
            List<InfoField> results = new List<InfoField>();

            foreach (XmlNode c in e.ChildNodes)
                if (c.NodeType == XmlNodeType.Element)
                    results.Add(FromElement((XmlElement)c));

            return results;
        }

        public static InfoField FromElement(XmlElement e)
        {
            return new InfoField
            {
                field = e.GetChildText("field"),
                items = InfoItem.FromContainerElement(e)
            };
        }
    }

    [DataContract, Serializable]
    public class InfoItem
        : FacebookEntity
    {
        [DataMember]
        public string label { get; set; }
        [DataMember]
        public string link { get; set; }
        [DataMember]
        public string image { get; set; }
        [DataMember]
        public string description { get; set; }
        [DataMember]
        public string sublabel { get; set; }

        public static IList<InfoItem> FromContainerElement(XmlElement e)
        {
            var results = new List<InfoItem>();
            foreach (XmlNode c in e)
                if (c.NodeType == XmlNodeType.Element)
                    results.Add(FromElement((XmlElement)c));
            return results;
        }

        public static InfoItem FromElement(XmlElement e)
        {
            return new InfoItem
            {
                label = e.GetChildText("label"),
                link = e.GetChildText("link"),
                image = e.GetChildText("image"),
                description = e.GetChildText("description"),
                sublabel = e.GetChildText("sublabel")
            };
        }

        public static IList<InfoItem> FromFqlResult(XmlDocument fqlResult, bool keepXml)
        {
            return fqlResult.GetFqlResults(keepXml, e => FromElement(e));
        }
    }
}
